from flask_wtf import FlaskForm

from wtforms import StringField, IntegerField
from wtforms.validators import DataRequired, Length, ValidationError, EqualTo

from app.models import User


class RegisterValidate(FlaskForm):

    username = StringField('用户名',validators=[
        DataRequired(message='请填写用户名'),
        Length(2,20,message='用户名为2到20个字符')
    ])
    password = StringField('密码',validators=[
        DataRequired(message='密码未填写'),
        Length(6,50,message='密码为6到50个字符')
    ])
    repassword = StringField('确认密码',validators=[
        DataRequired(message='确认密码必填'),
        Length(6,50,message='确认密码为6到50个字符'),
        EqualTo('password',message='前后密码不一致')
        ])
    # age = IntegerField('年龄',validators=[Length()])

    def validate_username(self,field):
        user = User.query.filter(User.name==field.data).first()
        if user:
            raise ValidationError('用户已存在')

class LoginValidate(FlaskForm):
    username = StringField('用户名', validators=[
        DataRequired(message='请填写用户名'),
        Length(2, 20, message='用户名为2到20个字符')
    ])
    password = StringField('密码', validators=[
        DataRequired(message='密码未填写'),
        Length(6, 50, message='密码为6到50个字符')
    ])
    def validate_username(self,field):
        user = User.query.filter(User.name==field.data).first()
        if not user:
            raise ValidationError('用户不存在')

class AddArticle(FlaskForm):
    pass
